package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentServiceDetailDescribeBinding;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.view.ui.activity.enterprise.ProfessionalQuoteDetailActivity;
import com.sgb.kjwl.view.widget.ScrollWebView;

public class ServiceDetailDescribeFragment extends AppFragment {
    private FragmentServiceDetailDescribeBinding binding;

    public static ServiceDetailDescribeFragment newInstance(EpQuoteEntity entity) {
        ServiceDetailDescribeFragment fragment = new ServiceDetailDescribeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", entity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_service_detail_describe, container, false);
        ((ProfessionalQuoteDetailActivity) getActivity()).setChildObjectForPosition(binding.getRoot(), 0);

//        binding.webDescription.setOnTouchListener(new View.OnTouchListener() {
//            @SuppressLint("ClickableViewAccessibility")
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_UP) {
//                    ((ProfessionalQuoteDetailActivity) getActivity()).setScroll(false);
//                } else {
//                    ((ProfessionalQuoteDetailActivity) getActivity()).setScroll(true);
//                }
//                return false;
//            }
//        });

        binding.webDescription.setOnTouchListener(new ScrollWebView.OnTouchListener() {
            @Override
            public void onTouchStart() {
                ((ProfessionalQuoteDetailActivity) getActivity()).setScroll(false);
            }

            @Override
            public void onTouchEnd() {
                ((ProfessionalQuoteDetailActivity) getActivity()).setScroll(true);
            }
        });
        binding.webDescription.setOnScrollChangeListener(new ScrollWebView.OnScrollChangeListener() {
            @Override
            public void onPageEnd(int l, int t, int oldl, int oldt) {
                ((ProfessionalQuoteDetailActivity) getActivity()).setScroll(false);
            }

            @Override
            public void onPageTop(int l, int t, int oldl, int oldt) {
                ((ProfessionalQuoteDetailActivity) getActivity()).setScroll(false);
            }

            @Override
            public void onScrollChanged(int l, int t, int oldl, int oldt) {

            }
        });
        return binding.getRoot();
    }


    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        EpQuoteEntity entity = (EpQuoteEntity) getArguments().getSerializable("entity");

        String content = entity.getDescription();
        if (content != null && !content.equals("")) {
            binding.setEntity(entity);
//            content = content.replace("<img", "<img style=max-width:100%;height:auto");
            binding.webDescription.getSettings().setDefaultTextEncodingName("UTF-8");
            WebSettings webSettings = binding.webDescription.getSettings();
            webSettings.setUseWideViewPort(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            binding.webDescription.setHorizontalScrollBarEnabled(false);
            binding.webDescription.setVerticalScrollBarEnabled(false);
            binding.webDescription.loadDataWithBaseURL(null, setWebViewContent(content), "text/html", "UTF-8", null);
            binding.webDescription.requestLayout();
            binding.webDescription.requestLayout();
            binding.emptyView.setVisibility(View.GONE);
        } else {
            binding.emptyView.setVisibility(View.VISIBLE);
        }
//        if (entity.getImage() != null && !entity.getImage().equals("")) {
//            Glide.with(getContext()).load(entity.getImage()).into(binding.ivTeamDescribe);
//        } else {
//            Glide.with(getContext()).load(R.mipmap.morentu).into(binding.ivTeamDescribe);
//        }
    }

    /**
     * 适配image和table标签
     */
    private static String setWebViewContent(String content) {
        String head = "<style type=\"text/css\"> img {" +
                "width:100%;" +//限定图片宽度填充屏幕
                "height:auto;" +//限定图片高度自动
                "}" +
                "body {" +
                "margin-right:15px;" +//限定网页中的文字右边距为15px(可根据实际需要进行行管屏幕适配操作)
                "margin-left:30px;" +//限定网页中的文字左边距为15px(可根据实际需要进行行管屏幕适配操作)
                "margin-top:15px;" +//限定网页中的文字上边距为15px(可根据实际需要进行行管屏幕适配操作)
                "font-size:38px;" +//限定网页中文字的大小为40px,请务必根据各种屏幕分辨率进行适配更改
                "word-wrap:break-word;" +//允许自动换行(汉字网页应该不需要这一属性,这个用来强制英文单词换行,类似于word/wps中的西文换行)
                "}" +
                "</style>";
        return "<html><header>" + head + "</header>" + content + "</html>";
    }
}
